package com.ikas.ai.server.module.morningpaper.model.elements.wholeyear;

import com.ikas.ai.server.module.morningpaper.consts.MorningPaperConditionWholeYearVariableNames;
import com.ikas.ai.server.module.morningpaper.consts.MorningPaperFailRecordVariableNames;
import com.ikas.ai.server.module.morningpaper.exception.MorningPaperException;
import com.ikas.ai.server.module.morningpaper.model.dto.MorningPaperVariablesDto;
import com.ikas.ai.server.module.morningpaper.model.elements.MorningPaperTopElement;

import java.math.BigDecimal;

public abstract class AbstractElementMachineConditionWholeYearSuccessPercent extends MorningPaperTopElement {

    /**
     * 获取失败记录次数
     */
    protected Integer getFailCountByConditionName(MorningPaperFailRecordVariableNames morningPaperFailRecordVariableNames){
        return Integer.valueOf(this.getPoMap().get(morningPaperFailRecordVariableNames.name()).getValue());
    }

    /**
     * 计算全年成功失败百分比：
     * 成功数/成功数+失败数
     * @param element
     * @param mv
     * @return
     */
    protected Double fillWholeYearSuccessPercent(AbstractElementMachineConditionWholeYearSuccessPercent element, MorningPaperVariablesDto mv){
        try {
            if (MorningPaperConditionWholeYearVariableNames.conditionGInWholeYearSuccessPercent.name().equals(element.getMorningPaperVariableName())){
                BigDecimal successCount = new BigDecimal(mv.getConditionGInWholeYearCount());
                BigDecimal failCount = new BigDecimal(
                        getFailCountByConditionName(
                                MorningPaperFailRecordVariableNames.failCountOfConditionGInWholeYear)
                );
                return successCount.divide(successCount.add(failCount), 2, BigDecimal.ROUND_HALF_UP).doubleValue();
            }
            else if (MorningPaperConditionWholeYearVariableNames.conditionPInWholeYearSuccessPercent.name().equals(element.getMorningPaperVariableName())){
                BigDecimal successCount = new BigDecimal(mv.getConditionPInWholeYearCount());
                BigDecimal failCount = new BigDecimal(
                        getFailCountByConditionName(
                                MorningPaperFailRecordVariableNames.failCountOfConditionPInWholeYear)
                );
                return successCount.divide(successCount.add(failCount), 2, BigDecimal.ROUND_HALF_UP).doubleValue();
            }
            else if (MorningPaperConditionWholeYearVariableNames.conditionPCInWholeYearSuccessPercent.name().equals(element.getMorningPaperVariableName())){
                BigDecimal successCount = new BigDecimal(mv.getConditionGInWholeYearCount());
                BigDecimal failCount = new BigDecimal(
                        getFailCountByConditionName(
                                MorningPaperFailRecordVariableNames.failCountOfConditionPCInWholeYear)
                );
                return successCount.divide(successCount.add(failCount), 2, BigDecimal.ROUND_HALF_UP).doubleValue();
            }
        } catch (NullPointerException e) {
            throw new MorningPaperException(String.format(MorningPaperException.needsFieldIsEmpty,mv.toString()));
        }
        return null;
    }
}
